<template>
  <div>
    <BasicTable @register="registerTable" @resize-column="handleResizeColumn">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate"> 新增 </a-button>
        <a-button type="primary" @click="handleMoreDelete"> 删除 </a-button>
      </template>

      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              icon: 'clarity:note-edit-line',
              onClick: handleEdit.bind(null, record),
            },
            {
              icon: 'ant-design:delete-outlined',
              color: 'error',
              popConfirm: {
                title: '是否确认删除',
                placement: 'topRight',
                confirm: handleDelete.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>
    <AutoCodeDrawer @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  import { defineComponent, h } from 'vue';

  import { BasicTable, useTable, TableAction } from '/@/components/Table';

  import { useDrawer } from '/@/components/Drawer';
  import AutoCodeDrawer from './AutoCodeDrawer.vue';

  import { columns, searchFormSchema } from './autoCode.data';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useI18n } from '/@/hooks/web/useI18n';
  const { t } = useI18n();
  import { Select, Input } from 'ant-design-vue';

  import { getFlowSequenceList, getFlowSequenceDel } from '/@/api/flowManage/flowManage';
  export default defineComponent({
    name: 'Notice',
    components: {
      BasicTable,
      AutoCodeDrawer,
      TableAction,
      [Select.name]: Select,
      ASelectOption: Select.Option,
      [Input.name]: Input,
      [Input.Group.name]: Input.Group,
    },
    setup() {
      const { createMessage, createConfirm } = useMessage();
      const [registerDrawer, { openDrawer }] = useDrawer();

      const [registerTable, { reload, getSelectRowKeys, setSelectedRowKeys }] = useTable({
        title: '自动编号',
        api: getFlowSequenceList,
        columns,
        formConfig: {
          schemas: searchFormSchema,
        },
        searchInfo: {
          op: 'search',
        }, //额外的参数
        useSearchForm: true,
        showTableSetting: true,
        bordered: true,
        indexColumnProps: { width: 50 },
        showIndexColumn: false,
        beforeFetch: (info) => {
          let newInfo = info;
          if (newInfo.dates && newInfo.dates.length > 0) {
            newInfo.fromDate = newInfo.dates[0];
            newInfo.toDate = newInfo.dates[1];
            delete newInfo.dates;
          }
          return newInfo;
        },
        rowKey: 'id',
        rowSelection: {
          type: 'checkbox',
        },
        actionColumn: {
          width: 120,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
          fixed: 'right',
        },
      });

      function handleCreate() {
        openDrawer(true, {
          isUpdate: false,
        });
      }

      function handleEdit(record: Recordable) {
        openDrawer(true, {
          record,
          isUpdate: true,
        });
      }

      const handleMoreDelete = () => {
        const selectRowKeys = getSelectRowKeys();
        if (selectRowKeys.length == 0) {
          return createMessage.warning('请选择数据');
        }
        const ids = selectRowKeys.join(',');
        deleteAction(ids);
      };

      const handleDelete = (record: Recordable) => {
        deleteAction(record.id);
      };

      const deleteAction = (ids) => {
        createConfirm({
          iconType: 'warning',
          title: () => h('span', t('common.prompt')),
          content: () => h('span', '您确定要删除么'),
          onOk: async () => {
            try {
              const params = {
                ids: ids,
              };
              await getFlowSequenceDel(params);
              createMessage.success('操作成功');
              handleSuccess();
            } catch (err) {
              console.error(err);
            } finally {
            }
          },
        });
      };

      function handleSuccess() {
        setSelectedRowKeys([]);
        reload();
      }
      return {
        registerTable,
        registerDrawer,
        handleCreate,
        handleEdit,
        handleMoreDelete,
        handleDelete,
        handleSuccess,
        handleResizeColumn: (w, col) => {
          col.width = w;
        },
      };
    },
  });
</script>
